/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ru.rantipin.GUI;

import com.sun.media.renderer.video.BasicVideoRenderer.CompListener;
import com.toedter.calendar.JDateChooser;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.MouseInputAdapter;
import ru.rantipin.persistence.PersistenceHelper;
import ru.rantipin.persistence.Production;
import ru.rantipin.persistence.Research;
import ru.rantipin.persistence.Sample;

/**
 *
 * @author rantipin
 */
public class ResearchDataBasePanel extends JPanel {

    public ResearchDataBasePanel() {
        initComponentsLayout();
    }

    public void loadResearchData() {
        researches = PersistenceHelper.getInstance().findReseachesByDate(getStartDate().getDate(), getEndDate().getDate());
        
        getResearchValues().removeAllItems();
        
        if (!researches.isEmpty()) {
            selectedResearche = 0;
            for (Research research : researches) {
                getResearchValues().addItem(research.getName());
            }
            loadResearchPanelByIndex(selectedResearche);
        } else {
            selectedResearche = -1;
            selectedSample = -1;
            cleanResearchPanel();
        }
    }

    private void cleanResearchPanel() {
        getDateText().setText("");
        getDescriptionTextArea().setText("");
        getCommentTextArea().setText("");
        getSampleValues().removeAllItems();
        samples.clear();
        selectedSample = -1;
    }

    private void loadResearchPanelByIndex(int index) {
        Research research = researches.get(index);
        getDateText().setText(DateFormat.getDateInstance().format(research.getDate()));
        getDescriptionTextArea().setText(research.getDescription());
        getCommentTextArea().setText(research.getComment());

        for (Production production : research.getProductionCollection()) {
            for (Sample sample : production.getSampleCollection()) {
                samples.add(sample);
                getSampleValues().addItem(sample.getName());
            }
        }
        selectedSample = samples.isEmpty() ? -1: 0;
    }

    protected void initComponentsLayout() {
        GridBagLayout gridbag = new GridBagLayout();
        setLayout(gridbag);

        gridbag.setConstraints(getFilterPanel(),
                new GridBagConstraints(
                    0, 0,
                    GridBagConstraints.REMAINDER, 1,
                    0.0, 0.0,
                    GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL,
                    new Insets(0, 10, 0, 10),
                    0, 0
                ));
        add(getFilterPanel());

        JLabel researchNameLabel = new JLabel("Research name:");
        gridbag.setConstraints(researchNameLabel,
                new GridBagConstraints(
                    0, 1,
                    GridBagConstraints.REMAINDER, 1,
                    0.0, 0.0,
                    GridBagConstraints.LINE_START, GridBagConstraints.NONE,
                    new Insets(5, 10, 0, 10),
                    0, 0
                ));
        add(researchNameLabel);

        gridbag.setConstraints(getResearchValues(),
                new GridBagConstraints(
                    0, 2,
                    GridBagConstraints.REMAINDER, 1,
                    0.0, 0.0,
                    GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL,
                    new Insets(5, 10, 0, 10),
                    0, 0
                ));
        add(getResearchValues());

        JLabel dateLabel = new JLabel("Date of creation:");
        gridbag.setConstraints(dateLabel,
                new GridBagConstraints(
                    0, 3,
                    1, 1,
                    0.0, 0.0,
                    GridBagConstraints.LINE_START, GridBagConstraints.NONE,
                    new Insets(5, 10, 0, 10),
                    0, 0
                ));
        add(dateLabel);
        
        gridbag.setConstraints(getDateText(),
                new GridBagConstraints(
                    0, 3,
                    1, 1,
                    0.0, 0.0,
                    GridBagConstraints.LINE_END, GridBagConstraints.NONE,
                    new Insets(5, 10, 0, 10),
                    0, 0
                ));
        add(getDateText());

        JLabel descriptionLabel = new JLabel("Description:");
        gridbag.setConstraints(descriptionLabel,
                new GridBagConstraints(
                    0, 4,
                    1, 1,
                    0.0, 0.0,
                    GridBagConstraints.LINE_START, GridBagConstraints.NONE,
                    new Insets(5, 10, 0, 10),
                    0, 0
                ));
        add(descriptionLabel);

        JScrollPane descriptionArea = new JScrollPane(getDescriptionTextArea());
        descriptionArea.setMinimumSize(new Dimension(0, 60));
        descriptionArea.setPreferredSize(new Dimension(0, 60));
        gridbag.setConstraints(descriptionArea,
                new GridBagConstraints(
                    0, 5,
                    GridBagConstraints.REMAINDER, 1,
                    1.0, 1.0,
                    GridBagConstraints.LINE_START, GridBagConstraints.BOTH,
                    new Insets(5, 10, 0, 10),
                    0, 0
                ));
        add(descriptionArea);

        JLabel commentLabel = new JLabel("Comment:");
        gridbag.setConstraints(commentLabel,
                new GridBagConstraints(
                    0, 6,
                    1, 1,
                    0.0, 0.0,
                    GridBagConstraints.LINE_START, GridBagConstraints.NONE,
                    new Insets(5, 10, 0, 10),
                    0, 0
                ));
        add(commentLabel);

        JScrollPane commentArea = new JScrollPane(getCommentTextArea());
        commentArea.setMinimumSize(new Dimension(0, 60));
        commentArea.setPreferredSize(new Dimension(0, 60));
        gridbag.setConstraints(commentArea,
                new GridBagConstraints(
                    0, 7,
                    GridBagConstraints.REMAINDER, 1,
                    1.0, 1.0,
                    GridBagConstraints.LINE_START, GridBagConstraints.BOTH,
                    new Insets(5, 10, 0, 10),
                    0, 0
                ));
        add(commentArea);

        JLabel sampleNameLabel = new JLabel("Film sample name:");
        gridbag.setConstraints(sampleNameLabel,
                new GridBagConstraints(
                    0, 8,
                    1, 1,
                    0.0, 0.0,
                    GridBagConstraints.LINE_START, GridBagConstraints.NONE,
                    new Insets(5, 10, 0, 10),
                    0, 0
                ));
        add(sampleNameLabel);

        gridbag.setConstraints(getSampleValues(),
                new GridBagConstraints(
                    0, 9,
                    GridBagConstraints.REMAINDER, 1,
                    0.0, 0.0,
                    GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL,
                    new Insets(5, 10, 0, 10),
                    0, 0
                ));
        add(getSampleValues());
    }

    private JPanel getFilterPanel() {
        JPanel filterPanel = new JPanel();
        filterPanel.setBorder(BorderFactory.createTitledBorder("Research date filter"));
        GridBagLayout gridbag = new GridBagLayout();
        filterPanel.setLayout(gridbag);

        JLabel startLabel = new JLabel("Start date:");
        gridbag.setConstraints(startLabel,
                new GridBagConstraints(
                    0, 0,
                    1, 1,
                    0.0, 0.0,
                    GridBagConstraints.LINE_START, GridBagConstraints.NONE,
                    new Insets(0, 10, 0, 50),
                    0, 0
                ));
        filterPanel.add(startLabel);

        gridbag.setConstraints(getStartDate(),
                new GridBagConstraints(
                    1, 0,
                    1, 1,
                    0.0, 0.0,
                    GridBagConstraints.LINE_END, GridBagConstraints.HORIZONTAL,
                    new Insets(0, 0, 0, 10),
                    10, 0
                ));
        filterPanel.add(getStartDate());

        JLabel endLabel = new JLabel("End date:");
        gridbag.setConstraints(endLabel,
                new GridBagConstraints(
                    0, 1,
                    1, 1,
                    0.0, 0.0,
                    GridBagConstraints.LINE_START, GridBagConstraints.NONE,
                    new Insets(0, 10, 0, 50),
                    0, 0
                ));
        filterPanel.add(endLabel);

        gridbag.setConstraints(getEndDate(),
                new GridBagConstraints(
                    1, 1,
                    1, 1,
                    0.0, 0.0,
                    GridBagConstraints.LINE_END, GridBagConstraints.HORIZONTAL,
                    new Insets(0, 0, 0, 10),
                    10, 0
                ));
        filterPanel.add(getEndDate());

        gridbag.setConstraints(getButtonFilter(),
                new GridBagConstraints(
                    0, 2,
                    GridBagConstraints.REMAINDER, 1,
                    0.0, 0.0,
                    GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
                    new Insets(5, 0, 0, 0),
                    0, 0
                ));
        filterPanel.add(getButtonFilter());
        
        return filterPanel;
    }

    private JButton getButtonFilter() {
        if (filter == null) {
            filter = new JButton("Filter");
            filter.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    loadResearchData();
                }
            });
        }
        return filter;
    }

    public JTextField getDateText() {
        if (dateText == null) {
            dateText = new JTextField(6);
            dateText.setEditable(false);
        }
        return dateText;
    }

    public JComboBox getResearchValues() {
        if (researchValues == null) {
            researchValues = new JComboBox();
            researchValues.setMaximumRowCount(5);
            researchValues.setEditable(false);
            researchValues.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    selectedResearche = researchValues.getSelectedIndex();
                    cleanResearchPanel();
                    if (selectedResearche != -1) {
                        loadResearchPanelByIndex(selectedResearche);
                    }
                }
            });
        }
        return researchValues;
    }

    public JComboBox getSampleValues() {
        if (sampleValues == null) {
            sampleValues = new JComboBox();
            sampleValues.setMaximumRowCount(5);
            sampleValues.setEditable(false);
            sampleValues.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    selectedSample = sampleValues.getSelectedIndex();
                }
            });
        }
        return sampleValues;
    }

    public JTextArea getDescriptionTextArea() {
        if (discriptionTextArea == null) {
                discriptionTextArea = generateTextArea();
        }
        return discriptionTextArea;
    }

    public JTextArea getCommentTextArea() {
        if (commentTextArea == null) {
                commentTextArea = generateTextArea();
        }
        return commentTextArea;
    }

    private JDateChooser getStartDate() {
        if (startDate == null) {
            startDate = generateDataChooser();
        }
        return startDate;
    }

    private JDateChooser getEndDate() {
        if (endDate == null) {
            endDate = generateDataChooser();
            
        }
        return endDate;
    }

    private JDateChooser generateDataChooser() {
        JDateChooser dateChooser = new JDateChooser("dd.MM.yyyy", "##.##.####", '_');
        dateChooser.setDate(new Date());
        return dateChooser;
    }

    private JTextArea generateTextArea() {
        JTextArea textArea = new JTextArea();
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        textArea.setEditable(false);
        return textArea;
    }

    public Sample getSelectedSample() throws Exception {
        if (selectedSample != -1) {
            return samples.get(selectedSample);
        } else {
            throw new Exception("Sample to save not selected");
        }
    }

    private ArrayList<Sample> samples = new ArrayList<Sample>();
    private ArrayList<Research> researches = null;
    private int selectedResearche = -1;
    private int selectedSample = -1;

    private JComboBox sampleValues = null;
    private JComboBox researchValues = null;
    private JTextArea discriptionTextArea = null;
    private JTextArea commentTextArea = null;
    private JTextField dateText = null;
    private JDateChooser startDate = null;
    private JDateChooser endDate = null;

    private JButton filter = null;
}
